package cn.iocoder.yudao.module.of.controller.admin.taskinfo;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.IdReq;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.of.controller.admin.taskinfo.vo.*;
import cn.iocoder.yudao.module.of.service.taskinfo.TaskInfoService;
import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptTreeVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 任务模块")
@RestController
@RequestMapping("/of/task-info")
@Validated
public class TaskInfoController {

    @Resource
    private TaskInfoService taskInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建任务模块")
    public CommonResult<Long> createTaskInfo(@Valid @RequestBody TaskInfoAddReqVO createReqVO) {
        return success(taskInfoService.createTaskInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新任务模块")
    public CommonResult<Boolean> updateTaskInfo(@Valid @RequestBody TaskInfoUpdateReqVO updateReqVO) {
        taskInfoService.updateTaskInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除任务模块")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteTaskInfo(@RequestParam("id") Long id) {
        taskInfoService.deleteTaskInfo(id);
        return success(true);
    }

    @PostMapping("/page")
    @Operation(summary = "获得任务模块分页")
    public CommonResult<PageResult<TaskInfoRespVO>> getTaskInfoPage(@Valid @RequestBody TaskInfoPageReqVO pageReqVO) {
        PageResult<TaskInfoRespVO> pageResult = taskInfoService.getTaskInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TaskInfoRespVO.class));
    }

    @GetMapping("/getDeptTree")
    @Operation(summary = "获得部门树")
    public CommonResult<List<DeptTreeVO>> getDeptTree() {

        List<DeptTreeVO> deptTree = taskInfoService.getDeptTree();

        return success(deptTree);
    }

    @GetMapping("/getMasterTaskDept")
    @Operation(summary = "获得主办任务的部门信息")
    public CommonResult<List<TaskDeptTreeVO>> getMasterTaskDept() {

        List<TaskDeptTreeVO> res = taskInfoService.getMasterTaskDept();

        return success(res);
    }


    @GetMapping("/getTaskCode")
    @Operation(summary = "获得任务编号")
    public CommonResult<String> getTaskCode() {

        String taskCode = taskInfoService.getTaskCode();

        return success(taskCode);
    }


    @PostMapping("/submitApprovalTask")
    @Operation(summary = "提交审批")
    public CommonResult<Boolean> submitApprovalTask(@Valid @RequestBody IdReq idReq) {

        taskInfoService.submitApprovalTask(idReq.getId());

        return success(true);
    }

}